<?php

if(!$INCLUDE_PATH){
$INCLUDE_PATH = "./";
}
require_once($INCLUDE_PATH . "include/WebService2Db.php");

class GlobalModel{

public static function buildCvshopCategoryReq(&$ws){
    $cacheTime=3600 ;
    $table="cvshop_trade_category";
    $req = $ws->buildRequestParameter($table,'select',"*","categorys"," status = 1 ",' id ',NULL,NULL,NULL);
      if($cacheTime){
      $req -> setCacheKey( md5("cvshop.trade_category.all"));
      $req -> setCacheTime($cacheTime);
    }
}

  public static function getMultilevelCacheId($objectName,$id) {
    $dir1 = "";
    if($articleId < 10000) { //小于一万的ID用1
      $dir1 = "0";
      if($articleId < 100) { //小于一千的的二级目录用1
        $dir2 = "0";
      } else {
        $dir2 = substr($articleId, -4, 2);  
      }
    } else {
      $dir1 = substr($articleId, 0,-4);
      $dir2 = substr($articleId,-4, 2);
    }
    return $objectName."|".$dir1."|".$dir2."|".$id; //smarty中以“|”划分目录
  }
  
  function getTimeDifference($time_one,$time_two)
  {
    $stamp_one=strtotime($time_one);
    $stamp_two=strtotime($time_two);
    $time_diff=abs($stamp_one-$stamp_two);
    $diff_time=getdate($time_diff);
    $diff_time["year"]-=1970;
    $diff_time["mon"]-=1;
    $diff_time["mday"]-=1;
    // ar_dump($diff_time);
    return $diff_time["mday"]."日".$diff_time["hours"]."时".$diff_time["minutes"]."分".$diff_time["seconds"];
  }


  /*
  *加载fck
  */
	public static function loadFck($input_name, $input_value=null)
	{

    require_once($GLOBALS['INCLUDE_PATH']."./app/plugins/fckeditor/fckeditor.php");
		
		$editor = new FCKeditor($input_name) ;
		$editor->BasePath   = $GLOBALS['INCLUDE_PATH']. "app/plugins/fckeditor/";
//		$editor->ToolbarSet = "Default";
//		$editor->Width      = "100%";
//		$editor->Height     = "400";
		$editor->Value      = $input_value;
//		$oFCKeditor->InstanceName = $smartyOutput;
		$c = $editor->CreateHtml();
    return $c;
	}

  /*
   *加载供求相关的下拉框配置
   */
  public static function loadTradeSel(){
    return array(
      "packaging"=>self::loadTradePackaging(),
      "priceType"=>self::loadTradePriceType(),
      "agio"=>self::loadTradeAgio()
    );
  }

  /*
   *加载供求包装下拉框配置
   */
  public static function loadTradePackaging(){
    return array(
      "箱"=>"箱",
      "包"=>"包",
      "盒"=>"盒",
      "桶"=>"桶",
      "瓶"=>"瓶",
      "罐"=>"罐",
      "坛"=>"坛",
      "缸"=>"缸",
      "捆"=>"捆",
      "散装"=>"散装"
    );
  }

  /*
   *加载购求价格类型
   */
   public static function loadTradePriceType(){
     return array(
      "个"=>"个",
      "件"=>"件",
      "台"=>"台",
      "只"=>"只",
      "把"=>"把",
      "双"=>"双",
      "条"=>"条",
      "盒"=>"盒",
      "套"=>"套",
      "斤"=>"斤",
      "座"=>"座"
     );

   }

    /*
     *加载供应折扣
     */
   public static function loadTradeAgio(){
     return array(
       "9.5"=>"9.5",
       "9"=>"9",
       "8.5"=>"8.5",
       "8"=>"8",
       "7.5"=>"7.5",
       "7"=>"7",
       "6.5"=>"6.5",
       "6"=>"6",
       "5.5"=>"5.5",
       "5"=>"5",
       "4.5"=>"4.5",
       "4"=>"4",
       "3.5"=>"3.5",
       "3"=>"3",
       "2.5"=>"2.5",
       "2"=>"2",
       "1.5"=>"1.5",
       "1"=>"1",
       "0.5"=>"0.5"

     ); 
   }
  /**
   * @param array()       $par
   * @param int           $total
   * @param int           $page
   * @param int           $size
   * @return string
   */
  static function listPage($par, $total, $page, $size, &$sortUrl = "") {
    $par_string = "";
    $sort_string = "";
    foreach ( $par as $key => $val ) {
      if (trim ( $val ) == "")
        continue;
      $par_string .= "&" . trim ( $key ) . "=" . trim ( $val );

      //给排序用的
      if ($key == "order_key" || $key == "order_meth")
        continue;
      $sort_string .= "&" . trim ( $key ) . "=" . trim ( $val );
    }
    $link = $_SERVER ['PHP_SELF'] . "?ctl=" . $_REQUEST ['ctl'] . "&act=" . $_REQUEST ['act'] . $par_string;
    $sortUrl = $_SERVER ['PHP_SELF'] . "?ctl=" . $_REQUEST ['ctl'] . "&act=" . $_REQUEST ['act'] . $sort_string . "&p=" . $page;

    /* 第一页 */
    $first = ($page == 1) ? "<span id='page_first' class='page_cur_num'>第一页</span>" : "<span id='page_first' class='page_num'><a href='" . $link . "&p=1'>第一页</a></span>";
    /* 最后一页 */
    if (ceil ( $total / $size ) == 1) {
      $last = "";
    } else {
      $last = ceil ( $total / $size ) <= $page ? "<span id='page_last' class='page_cur_num'>末页</span>" : "<span id='page_last' class='page_num'><a href='" . $link . "&p=" . ceil ( $total / $size ) . "'>末页</a></span>";
    }

    $pre = $page > 1 ? "<span id='pre_page'><a href='" . $link . "&p=" . ($page - 1) . "'>上一页</a></span>" : "<span id='pre_page'>上一页</span>";
    $next = $page < ceil ( $total / $size ) ? "<span id='pre_page'><a href='" . $link . "&p=" . ($page + 1) . "'>下一页</a></span>" : "<span id='pre_page'>下一页</span>";

    /* 中间部份 */
    $middle = "";
    $front = $page - 4;
    $after = $page + 4;
    for($i = $front; $i <= $after; $i ++) {

      if ($i <= 1 || $i >= ceil ( $total / $size )) {
        continue;
      }
      $middle .= "&nbsp;<a href='" . $link . "&p=" . $i . "'";
      $middle .= ($page == $i) ? "onmouseover='this.style.fontColor=\"red\"' class='page_cur_num'" : " class='page_num'";
      $middle .= ">" . $i . "</a>";
    }

    $goto = "";
    $goto .= "<span id='page_goto'>转到";
    $goto .= "<input type='text' id='goto_page' value='" . $page . "' onfocus='this.select();'" . " size='2' onblur=\"window.location.href='" . $link . "&p='+this.value+''\" />页";
    $goto .= "<input type='button' value='go'" . " onclick=\"window.location.href='" . $link . "&p='+document.getElementById('goto_page').value+''\" />";

    return "<span id='page_total'>共取得" . $total . "条数据，合计" . ceil ( $total / $size ) . "页。&nbsp;" . " - " . $first . "&nbsp;" . $pre . "&nbsp;" . $middle . "&nbsp;" . $next . "&nbsp;" . $last . "&nbsp;";
    //.$goto;
  }


}

?>
